﻿namespace SalesPortal.Web.Helpers
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Security.Cryptography;
    using System.Web;

    public interface ISymmetricCryptography
    {
        string Encrypt(string plainText, byte[] keyBytes, byte[] ivBytes);
        string Encrypt(string plainText);
        string Decrypt(string hexCipherText, byte[] keyBytes, byte[] ivBytes);
        string Decrypt(string hexCipherText);
    }

    public class DES 
    {
        public static string Encrypt(string plainText, byte[] keyBytes, byte[] ivBytes)
        {
            if (string.IsNullOrEmpty(plainText.Trim()))
                throw new ArgumentNullException("plainText");

            if (keyBytes == null || keyBytes.Length <= 0)
                throw new ArgumentNullException("keyBytes");

            if (ivBytes == null || ivBytes.Length <= 0)
                throw new ArgumentNullException("ivBytes");

            DESCryptoServiceProvider csp = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
            csp.Key = keyBytes;
            csp.IV = ivBytes;
            System.IO.MemoryStream ms = null;
            using (ms = new MemoryStream())
            using (CryptoStream cs = new CryptoStream(ms, csp.CreateEncryptor(), CryptoStreamMode.Write))
            using (StreamWriter sw = new StreamWriter(cs))
            {
                sw.AutoFlush = true;
                sw.Write(plainText);
            }

            return Help.Translate.BytesToHexString(ms.ToArray());
        }

        public static string Encrypt(string plainText)
        {
            if (string.IsNullOrEmpty(plainText.Trim()))
                throw new ArgumentNullException("plainText");

            return Encrypt(
                plainText,
                Convert.FromBase64String(System.Configuration.ConfigurationManager.AppSettings["Cryptography_Key"]),
                Convert.FromBase64String(System.Configuration.ConfigurationManager.AppSettings["Cryptography_IV"]));
        }

        public static string Decrypt(string hexCipherText, byte[] keyBytes, byte[] ivBytes)
        {
            if (string.IsNullOrEmpty(hexCipherText.Trim()))
                throw new ArgumentNullException("hexCipherText");

            if (keyBytes == null || keyBytes.Length <= 0)
                throw new ArgumentNullException("keyBytes");

            if (ivBytes == null || ivBytes.Length <= 0)
                throw new ArgumentNullException("ivBytes");

            string result = string.Empty;
            byte[] cipherBytes = Help.Translate.HexStringToBytes(hexCipherText);

            if (cipherBytes != null)
            {
                DESCryptoServiceProvider csp = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
                csp.Key = keyBytes;
                csp.IV = ivBytes;
                System.IO.MemoryStream ms = null;
                using (ms = new MemoryStream(cipherBytes))
                using (CryptoStream cs = new CryptoStream(ms, csp.CreateDecryptor(), CryptoStreamMode.Read))
                using (StreamReader sr = new StreamReader(cs))
                {
                    result = sr.ReadToEnd();
                }
            }

            return result;
        }

        public static string Decrypt(string hexCipherText)
        {
            if (string.IsNullOrEmpty(hexCipherText.Trim()))
                throw new ArgumentNullException("hexCipherText");

            return Decrypt(
                hexCipherText,
                Convert.FromBase64String(System.Configuration.ConfigurationManager.AppSettings["Cryptography_Key"]),
                Convert.FromBase64String(System.Configuration.ConfigurationManager.AppSettings["Cryptography_IV"]));
        }
    }

    public class AES 
    {
        public static string Encrypt(string plainText, byte[] keyBytes, byte[] ivBytes)
        {
            if (string.IsNullOrEmpty(plainText.Trim()))
                throw new ArgumentNullException("plainText");

            if (keyBytes == null || keyBytes.Length <= 0)
                throw new ArgumentNullException("keyBytes");

            if (ivBytes == null || ivBytes.Length <= 0)
                throw new ArgumentNullException("ivBytes");

            AesCryptoServiceProvider csp = (AesCryptoServiceProvider)AesCryptoServiceProvider.Create();
            csp.Key = keyBytes;
            csp.IV = ivBytes;
            System.IO.MemoryStream ms = null;
            using (ms = new MemoryStream())
            using (CryptoStream cs = new CryptoStream(ms, csp.CreateEncryptor(), CryptoStreamMode.Write))
            using (StreamWriter sw = new StreamWriter(cs))
            {
                sw.AutoFlush = true;
                sw.Write(plainText);
            }

            return Help.Translate.BytesToHexString(ms.ToArray());
        }

        public static string Encrypt(string plainText)
        {
            if (string.IsNullOrEmpty(plainText.Trim()))
                throw new ArgumentNullException("plainText");

            return Encrypt(
                plainText,
                Convert.FromBase64String(System.Configuration.ConfigurationManager.AppSettings["Cryptography_Key"]),
                Convert.FromBase64String(System.Configuration.ConfigurationManager.AppSettings["Cryptography_IV"]));
        }

        public static string Decrypt(string hexCipherText, byte[] keyBytes, byte[] ivBytes)
        {
            if (string.IsNullOrEmpty(hexCipherText.Trim()))
                throw new ArgumentNullException("hexCipherText");

            if (keyBytes == null || keyBytes.Length <= 0)
                throw new ArgumentNullException("keyBytes");

            if (ivBytes == null || ivBytes.Length <= 0)
                throw new ArgumentNullException("ivBytes");

            string result = string.Empty;
            byte[] cipherBytes = Help.Translate.HexStringToBytes(hexCipherText);

            if (cipherBytes != null)
            {
                AesCryptoServiceProvider csp = (AesCryptoServiceProvider)AesCryptoServiceProvider.Create();
                csp.Key = keyBytes;
                csp.IV = ivBytes;
                System.IO.MemoryStream ms = null;
                using (ms = new MemoryStream(cipherBytes))
                using (CryptoStream cs = new CryptoStream(ms, csp.CreateDecryptor(), CryptoStreamMode.Read))
                using (StreamReader sr = new StreamReader(cs))
                {
                    result = sr.ReadToEnd();
                }
            }

            return result;
        }

        public static string Decrypt(string hexCipherText)
        {
            if (string.IsNullOrEmpty(hexCipherText.Trim()))
                throw new ArgumentNullException("hexCipherText");

            return Decrypt(
                hexCipherText,
                Convert.FromBase64String(System.Configuration.ConfigurationManager.AppSettings["Cryptography_Key"]),
                Convert.FromBase64String(System.Configuration.ConfigurationManager.AppSettings["Cryptography_IV"]));
        }
    }
}